package com.shxc.smarttrade.old.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;
import com.shxc.smarttrade.tigerwit.activity.TigerwitOpenAccountActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/10/17.
 */

public class ChooseBrokerActivity extends BaseActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;

    private int userId;
    private String phone;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    String registerURL = (String) msg.obj;
                    Intent intent = new Intent(ChooseBrokerActivity.this, TigerwitOpenAccountActivity.class);
                    intent.putExtra("url", registerURL);
                    startActivity(intent);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_broker);
        initView();
    }

    private void initView() {
        tvCenter.setText("开设账户");
        userId = SpUtils.getInt(this, "userId", -1);
        phone = SpUtils.getString(this, "telephone");
    }

    @OnClick({R.id.rl_left, R.id.btn_open_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                finish();
                break;
            case R.id.btn_open_account:
                showShortDialog();
                getTangerForeURL(phone, userId);
                break;
        }
    }

    /**
     * 获取老虎外汇链接
     *
     * @param phone
     * @param userId
     */
    private void getTangerForeURL(final String phone, int userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("userId", userId + "");
        map.put("phone", phone);
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.ACCOUNT_FRAGMENT_REGISTERURL, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                showToast(getString(R.string.net_error));
                dismissShortDialog();
            }

            @Override
            public void success(String response, int id) {
                dismissShortDialog();
                if (response == null || new JsonParser().parse(response).isJsonNull()) {
                    showToast(getString(R.string.data_error));
                    return;
                }
                JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                String errCode = "";
                String errMsg = "";
                if (!jsonObject.get("err_code").isJsonNull())
                    errCode = jsonObject.get("err_code").getAsString();
                if (!jsonObject.get("err_msg").isJsonNull())
                    errMsg = jsonObject.get("err_msg").getAsString();
                if (errCode.equals("0")) {
                    JsonObject data = jsonObject.get("data").getAsJsonObject();
                    String registerURL = "";
                    if (!data.get("registerURL").isJsonNull())
                        registerURL = data.get("registerURL").getAsString();
                    Message msg = Message.obtain();
                    msg.what = 0;
                    msg.obj = registerURL;
                    handler.sendMessage(msg);
                } else {
                    showToast(errMsg);
                }
            }
        });
    }
}
